import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  { path: '*', redirect: '/404', hidden: true }
]

export const asyncRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [{
      path: 'home',
      name: 'home',
      component: () => import('@/views/home/index'),
      meta: { title: '首页', icon: 'home', roles: ['user', 'admin'] }
    }]
  },

  {
    path: '/setting',
    component: Layout,
    children: [{
      path: 'userinfo',
      name: 'userinfo',
      hidden: true,
      component: () => import('@/views/setting/userinfo'),
      meta: { title: '个人信息', roles: ['user', 'admin'] }
    }]
  },

  {
    path: '/course',
    component: Layout,
    redirect: '/course/list',
    name: 'Course',
    meta: { title: '课程管理', icon: 'course', roles: ['user', 'admin'] },
    children: [
      {
        path: 'list',
        name: 'CourseList',
        component: () => import('@/views/course/list'),
        meta: { title: '课程推荐', icon: 'commend', roles: ['user', 'admin'] }
      },
      {
        path: 'teacher',
        name: 'Teacher',
        component: () => import('@/views/course/teacher'),
        meta: { title: '教师数据', icon: 'teacher', roles: ['user', 'admin'] }
      },
      {
        path: 'detail',
        name: 'CourseDetail',
        hidden: true,
        component: () => import('@/views/course/detail'),
        meta: { title: '课程详情', roles: ['user', 'admin'] }
      }
    ]
  },

  {
    path: '/manage',
    component: Layout,
    redirect: '/manage/index',
    name: 'Manage',
    meta: {
      title: '数据管理',
      icon: 'life', roles: ['user', 'admin']
    },
    children: [
      {
        path: 'index',
        name: 'Manages',
        component: () => import('@/views/manage/index'),
        meta: { title: '数据分析', icon: 'data', roles: ['user', 'admin'] }
      },
      {
        path: 'benford',
        name: 'Benford',
        component: () => import('@/views/manage/benford'),
        meta: { title: '本福特定律分析', icon: 'man', roles: ['user', 'admin'] }
      },
      {
        path: 'life',
        name: 'Life',
        component: () => import('@/views/manage/life'),
        meta: { title: '生命点分析', icon: 'life', roles: ['user', 'admin'] }
      }
    ]
  },

  {
    path: '/update',
    component: Layout,
    redirect: '/update/index',
    name: 'Update',
    meta: {
      title: '数据更新',
      icon: 'update', roles: ['admin']
    },
    children: [
      {
        path: 'index',
        name: 'Form',
        component: () => import('@/views/update/index'),
        meta: { title: '动态爬取数据', icon: 'update', roles: ['admin'] }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/auth',
    name: 'User',
    meta: {
      title: '用户管理',
      icon: 'user', roles: ['user', 'admin']
    },
    children: [
      {
        path: 'auth',
        component: () => import('@/views/user/auth'),
        name: 'Auth',
        meta: { title: '用户管理', icon: 'auth', roles: ['admin'] }
      }
      // {
      //   path: 'survey',
      //   name: 'Survey',
      //   component: () => import('@/views/user/survey'),
      //   meta: { title: '用户反馈', icon: 'survey', roles: ['user', 'admin'] }
      // }
    ]
  }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
